#!/usr/bin/python
# -*- coding: utf-8 -*-

##
 # Copyright 2009 Caio Souza
 # This file is part of Rpyg.
 #
 # Rpyg is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # Rpyg is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with Rpyg. If not, see <http://www.gnu.org/licenses/>.
 ##

'''Módulo base de todos os mapas.'''

import copy

class Map:
    '''Classe base para os mapas do jogo.'''
    
    def __init__(self, matrix):
        '''Cria um mapa a partir da transposta da matriz passada.
        
        Cria uma matriz local com a estrutura transposta da matriz passada e
        com uma cópia rasa do conteúdo dela.
        '''
        
        self._matrix = []
        '''Matriz de dados do mapa.'''
        
        self._w = len(matrix[0])
        '''Largura do mapa.'''
        
        self._h = len(matrix)
        '''Altura do mapa.'''
        
        
        for i in range(self._w):
            row = []
            for j in range(self._h):
                objs = matrix[j][i]
                
                if not objs: row.append(objs)
                    
                else:
                    if not isinstance(objs, (list, tuple)):
                        objs = copy.copy(objs)
                        objs.i = i
                        objs.j = j
                        row.append(objs)
                        
                    else:
                        new_objs = []
                        for obj in objs:
                            obj = copy.copy(obj)
                            obj.i = i
                            obj.j = j
                            new_objs.append(obj)
                        row.append(new_objs)
                
            self._matrix.append(row)
            
    def __str__ (self):
        '''Representação em string do mapa.'''

        string = ''
        for i in range(self._h):
            for j in range(self._w):
                string += str(self._matrix[j][i]) + ' '
            string += '\n'
        return string
    
    def __iter__ (self):
        '''Retorna um objeto iterável sobre o mapa.'''
        
        iterlist = []
        for row in self._matrix: iterlist.extend(row)
        return iter(iterlist)
        